
#ifndef TYPES_H
#define TYPES_H

#if 0
#include <stdint.h>
#else

typedef unsigned char      uint8_t;
typedef unsigned short     uint16_t;
//typedef unsigned int     uint32_t;
typedef unsigned long      uint32_t;
typedef unsigned long long uint64_t;

typedef signed char        int8_t;
typedef signed short       int16_t;
typedef int                int32_t;
typedef long long          int64_t;

typedef unsigned int       uintptr_t;


#define UINT8_T    RETENTION uint8_t
#define UINT16_T   RETENTION uint16_t
#define UINT32_T   RETENTION uint32_t
#define UINT64_T   RETENTION uint64_t

#define INT8_T     RETENTION int8_t
#define INT16_T    RETENTION int16_t
#define INT32_T    RETENTION int32_t
#define INT64_T    RETENTION int64_t


/* Largest integral types.  */
#if __WORDSIZE == 64
typedef long int                 intmax_t;
typedef unsigned long int        uintmax_t;
#else
__extension__
typedef long long int            intmax_t;
__extension__
typedef unsigned long long int   uintmax_t;
#endif

//typedef unsigned int       size_t;

/* minimum values of exact-width signed integer types */
#define INT8_MIN                   -128
#define INT16_MIN                -32768
#define INT32_MIN          (~0x7fffffff)   /* -2147483648 is unsigned */
#define INT64_MIN		(-9223372036854775807LL - 1) /* -9223372036854775808 is unsigned */

/* maximum values of exact-width signed integer types */
#define INT8_MAX                    127
#define INT16_MAX                 32767
#define INT32_MAX            2147483647
#define INT64_MAX   9223372036854775807LL

/* maximum values of exact-width unsigned integer types */
#define UINT8_MAX                   255
#define UINT16_MAX                65535
#define UINT32_MAX           4294967295u
#define UINT64_MAX  0xffffffffffffffffULL

#endif

typedef unsigned char      bool_t;



#ifndef success
#define success       (0)
#endif
#ifndef SUCCESS
#define SUCCESS       (0)
#endif

#ifndef fail
#define fail          (1)
#endif
#ifndef FAIL
#define FAILE         (1)
#endif

#ifndef true
#define true          (1)
#endif
#ifndef TRUE
#define TRUE          (1)
#endif

#ifndef false
#define false         (0)
#endif
#ifndef FALSE
#define FALSE         (0)
#endif

#ifndef ENABLE
#define ENABLE        (1)
#endif

#ifndef DISABLE
#define DISABLE       (0)
#endif

#ifndef null
#define null          ((void *)0)
#endif
#ifndef NULL
#define NULL          ((void *)0)
#endif

#define COUNTOF(x)    (sizeof(x)/sizeof((x)[0]))

#define HI_U16(x)     (((x)>>8) & 0xFF)
#define LO_U16(x)     ((x) & 0xff)

#define MIN(x, y)     ((x) < (y) ? (x) : (y))
#define MAX(x, y)     ((x) > (y) ? (x) : (y))
#define min           MIN
#define max           MAX
#define BOUND(min, val, max)  MAX(min, MIN(max,val))

#define IS_POWER_OF_TWO(A)   ( ((A) != 0) && ((((A) - 1) & (A)) == 0) )

#define OFFSETOF(s, m)   ((unsigned int)&(( ((s)*)0 )->(m)))

#define BIT(x)      (1<<(x))

/**
 * @brief : Macro for performing rounded integer division (as opposed to truncating the result).
 * @param :  A   Numerator.
 * @param :  B   Denominator.
 * @return:  Rounded (integer) result of dividing A by B.
 */
#define ROUNDED_DIV(A, B) (((A) + ((B) / 2)) / (B))

#define MEM_ALIGNED_SIZE(SIZE, ALIGN)    ( ((SIZE) + ((ALIGN) - 1)) & ~((ALIGN)-1) )
#define MEM_ALIGN_ADDR(addr, MEM_ALIGN)  ((void *)(((uint32_t)(addr) + MEM_ALIGN - 1) & ~(uint32_t)(MEM_ALIGN - 1)))

#define MEM_ALIGNED4(size)  MEM_ALIGNED_SIZE(size, 4 )
#define MEM_ALIGNED8(size)  MEM_ALIGNED_SIZE(size, 8 )
#define MEM_ALIGNED16(size) MEM_ALIGNED_SIZE(size, 16)

#define UINT64(x)   ((uint64_t)x)

#endif
